package com.kaduoxee.ebooks.util;

import com.kaduoxee.ebooks.entity.Chapter;
import com.kaduoxee.ebooks.entity.ChapterContent;
import com.kaduoxee.ebooks.entity.Fiction;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;

public class JsoupUtil {

    private static String source = "https://www.81zw.com";


    @SuppressWarnings({"ConstantConditions", ""})
    public static Fiction writeFiction(Document doc) {
        Fiction fiction = new Fiction();
        Element con = doc.selectFirst("div.box_con");
        Element info = Objects.requireNonNull(con).selectFirst("#maininfo>#info");
        String bookName = info.selectFirst("h1").text();
        String aut = info.selectFirst("p").text();
//        作    者：王子凝渊
        String author = aut.split("：")[1];
        Element covEl = doc.selectFirst("#fmimg>img");
        String relCover = covEl.attr("src");
        URL url = doc.connection().request().url();
        source = url.getProtocol() + "://" + url.getHost();
        String cover = source + relCover;
        //intro
        Element introDiv = doc.selectFirst("#intro");
        String introduce = introDiv.selectFirst("p").text();
        //newest
        String newest = info.selectFirst(":containsOwn(最新章节)").selectFirst("a").text();
        //type
        Element top = con.selectFirst(".con_top");
        String typeFiction = top.select("a").get(1).text();
        String type = typeFiction.substring(0, typeFiction.length() - 2);
        //state
        Element stEl = doc.selectFirst(":containsOwn(状    态：)");
        String state = stEl.text().split(",")[0].split("：")[1];

        //保存fiction

        fiction.setBookName(bookName);
        fiction.setAuthor(author);
        fiction.setCover(cover);
        fiction.setIntroduce(introduce);
        fiction.setNewest(newest);
        fiction.setType(type);
        fiction.setState(state);
        fiction.setUrl(source + url.getPath());
        return fiction;
    }

    @SneakyThrows
    @SuppressWarnings("ConstantConditions")
    public static ChapterContent writeContent(String chapterUrl) {
        String url = source + chapterUrl;
        ChapterContent chapterContent = new ChapterContent();
        Document doc1 = Jsoup.connect(url).get();
        Element conDiv = doc1.selectFirst(".content_read").selectFirst("#content");
        String content = conDiv.text();
        chapterContent.setContent(content);
        return chapterContent;
    }


    public static Chapter writeChapter(Element a, Integer fictionId, Integer sort, Integer contentId) {
        Chapter chapter = new Chapter();
        chapter.setFictionId(fictionId);
        chapter.setChapterTitle(a.text());
        chapter.setSort(sort);
        chapter.setChapterUrl(source + a.attr("href"));
        chapter.setContentId(contentId);
        return chapter;
    }

    @SneakyThrows
    public static Chapter writeChapter(String chapterUrl, Integer fictionId, Integer sort, Integer contentId) {
        String chapterTitle = Jsoup.connect(chapterUrl).get().selectFirst(".bookname").child(0).text();
        Chapter chapter = new Chapter();
        chapter.setFictionId(fictionId);
        chapter.setChapterTitle(chapterTitle);
        chapter.setSort(sort);
        chapter.setChapterUrl(chapterUrl);
        chapter.setContentId(contentId);
        return chapter;
    }


}
